import { createSlice } from '@reduxjs/toolkit'

export const userSlice = createSlice({
  name: 'user',
  initialState: {
    username: null,
    router_permission: null,
    api_permission: null,
  },
  reducers: {
    loginIn: (state, action) => {
      state.username = action.payload.username;
      state.router_permission = action.payload.router_permission;
      state.api_permission = action.payload.api_permission;
    },
    loginOut: (state) => {
      state.username = null;
      state.router_permission = null;
      state.api_permission = null;
    }
  }
})

export const { loginIn, loginOut } = userSlice.actions
export const selectUser = state => state.user.username;
export const selectRouter_permission = state => state.user.router_permission;
export const selectApi_permission = state => state.user.api_permission;
export default userSlice.reducer